import cookie from 'js-cookie'

export default function ({ $axios, redirect }, inject) {

  $axios.onRequest(config => {
    const token = cookie.get('token')
    if (token) {
      config.headers.token = token
    }
    return config
  })
  $axios.onResponse(res => {
    return res;
  })
  $axios.onError(error => {
    const code = parseInt(error.response && error.response.status)
    if (code === 400) {
      redirect('/400')
    }
  })
}
